package com.xique.door.controller;

import com.xique.common.core.web.controller.BaseController;
import com.xique.common.core.web.domain.AjaxResult;
import com.xique.common.core.web.page.TableDataInfo;
import com.xique.common.log.annotation.Log;
import com.xique.common.log.enums.BusinessType;
import com.xique.common.security.annotation.PreAuthorize;
import com.xique.door.bean.domain.DAdvert;
import com.xique.door.bean.request.advert.AdvertAddRequest;
import com.xique.door.bean.request.advert.AdvertDownRequest;
import com.xique.door.bean.request.advert.AdvertSearchRequest;
import com.xique.door.bean.request.advert.AdvertUpdateRequest;
import com.xique.door.biz.service.IDoorAdvertBizService;
import com.xique.door.service.IDAdvertService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * 门禁广告Controller
 *
 * @author caogq
 * @date 2022-03-11
 */
@RestController
@RequestMapping("/advert")
public class DAdvertController extends BaseController {

    @Autowired
    private IDAdvertService dAdvertService;


    @Autowired
    private IDoorAdvertBizService advertBizService;

    /**
     * 查询门禁广告列表
     */
    @PreAuthorize(hasPermi = "oms:advert:list")
    @GetMapping("/list")
    public TableDataInfo list(AdvertSearchRequest request) {
        startPage();
        return advertBizService.conditionSearchAdvertList(request);
    }


    /**
     * 新增保存门禁广告
     */
    @PreAuthorize(hasPermi = "oms:advert:add")
    @Log(title = "门禁广告", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult addSave(@Valid AdvertAddRequest request, BindingResult bindingResult, HttpServletRequest request1) {
        if (bindingResult.hasErrors()) {
            return AjaxResult.error(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        return advertBizService.addAdvert(request);
    }


    /**
     * 修改保存门禁广告
     */
    @PreAuthorize(hasPermi = "oms:advert:edit")
    @Log(title = "门禁广告", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult editSave(AdvertUpdateRequest request) {
        return advertBizService.updateAdvert(request);
    }

    /**
     * 删除门禁广告
     */
    @PreAuthorize(hasPermi = "oms:advert:remove")
    @Log(title = "门禁广告", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    public AjaxResult remove(String ids) {
        return toAjax(dAdvertService.deleteDAdvertByIds(ids));
    }


    /**
     * 重发
     */
    @PreAuthorize(hasPermi = "oms:advert:edit")
    @Log(title = "门禁广告", businessType = BusinessType.UPDATE)
    @PostMapping("/down")
    public AjaxResult down(@RequestBody AdvertDownRequest request) {
        return advertBizService.down(request);
    }

    /**
     * 删除
     */
    @PreAuthorize(hasPermi = "oms:advert:edit")
    @PostMapping("/delete")
    public AjaxResult delete(@RequestBody AdvertDownRequest request) {
        return advertBizService.delete(request);
    }

    /**
     * 设备下架删除(单多选)
     */
    // @PreAuthorize(hasPermi = "oms:advert:edit")
    @PostMapping("/deleteOneDevice")
    public AjaxResult deleteOneDevice(AdvertDownRequest request) {
        if (request == null) {
            return AjaxResult.error("参数为空");
        }
        return advertBizService.deleteOne(request.getIds(), request.getAdvertId());
    }

    /**
     * 重发（单多选）
     */
    @PreAuthorize(hasPermi = "oms:advert:edit")
    @Log(title = "门禁广告", businessType = BusinessType.UPDATE)
    @PostMapping("/downs")
    public AjaxResult downs(AdvertDownRequest request) {
        if (request == null) {
            return AjaxResult.error("参数为空");
        }
        return advertBizService.downs(request.getIds(), request.getAdvertId());
    }


    /**
     * 详情
     */
    //  @PreAuthorize(hasPermi = "oms:advert:edit")
    @Log(title = "详情", businessType = BusinessType.UPDATE)
    @GetMapping("/details")
    public AjaxResult details(AdvertSearchRequest request) {
        return advertBizService.details(request);
    }

    /**
     * 删除门禁广告
     */
    @PreAuthorize(hasPermi = "oms:advert:edit")
    @Log(title = "门禁广告", businessType = BusinessType.UPDATE)
    @GetMapping("/deleteAdvert")
    public AjaxResult deleteAdvert(Long id) {
        return advertBizService.deleteAdvert(id);
    }
}
